//
//  FRBaseDataSource.m
//  Health
//
//  Created by mykoma on 14-9-7.
//  Copyright (c) 2014年 scu.edu. All rights reserved.
//

#import "LGBaseDataSource.h"
#import "FRHttpNetworking.h"


@interface LGBaseDataSource ()

@property (nonatomic, strong) NSString * serverUrl;
@property (nonatomic, strong) id<FRNetworking> networking;

@end

@implementation LGBaseDataSource

+ (instancetype)sharedInstance
{
    static LGBaseDataSource * sharedInstance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedInstance = [[LGBaseDataSource alloc] init];
    });
    return sharedInstance;
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        _serverUrl = @"http://114.215.127.64:8089";
        _networking = [FRHttpNetworking sharedInstance];
    }
    return self;
}

+(void)getAction:(NSString *)action
      parameters:(NSArray *)parameters
         success:(CompletionBlockWithDictionary)success
         failure:(CompletionBlockWithError)failure
{
    [[LGBaseDataSource sharedInstance].networking getFromServer:[LGBaseDataSource sharedInstance].serverUrl
                                                         action:action
                                                     parameters:parameters
                                                        success:success
                                                        failure:failure];
}

+(void)postAction:(NSString *)action
       parameters:(id)parameters
          success:(CompletionBlockWithObject)success
          failure:(CompletionBlockWithError)failure
{
    [[LGBaseDataSource sharedInstance].networking postToServer:[LGBaseDataSource sharedInstance].serverUrl
                                                        action:action
                                                    parameters:parameters
                                                       success:success
                                                       failure:failure];
}

+ (void)postAction:(NSString *)action
        parameters:(id)parameters
              data:(NSData *)data
          mimeType:(NSString *)mimeType
          fileName:(NSString *)fileName
           success:(CompletionBlockWithDictionary)success
           failure:(CompletionBlockWithError)failure
{
    [[LGBaseDataSource sharedInstance].networking postToServer:[LGBaseDataSource sharedInstance].serverUrl
                                                        action:action
                                                    parameters:parameters
                                                          data:data
                                                          name:@"file"
                                                      mimeType:mimeType
                                                      fileName:fileName
                                                       success:success
                                                       failure:failure];
}

@end
